package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/5/7 09:59
 * @apiNote
 */

public class Test00482licenseKeyFormatting {
    public String licenseKeyFormatting(String s, int k) {
        StringBuilder ans = new StringBuilder();
        int cnt = 0;
        for(int i = s.length() - 1; i >= 0; --i){
            if(s.charAt(i) != '-'){
                cnt++;
                ans.append(Character.toUpperCase(s.charAt(i)));
                if(cnt % k == 0){
                    ans.append("-");
                }
            }
        }
        if(ans.length() > 0 && ans.charAt(ans.length() - 1) == '-'){
            ans.deleteCharAt(ans.length()-1);
        }
        return ans.reverse().toString();
    }
}
